package com.miplus.ccrm.core.controller;

import com.miplus.ccrm.common.*;
import com.miplus.ccrm.core.controller.vo.credit.*;
import com.miplus.ccrm.core.controller.vo.ficase.*;
import com.miplus.ccrm.core.mapper.ConCustomerMapper;
import com.miplus.ccrm.core.pojo.ConCustomer;
import com.miplus.ccrm.core.pojo.FiCreditcases;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.collect.FiCreditcasesService;
import com.miplus.ccrm.core.service.crd.*;
import com.miplus.ccrm.core.utils.EnumFiCaseQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 2-4.信用卡类案件服务接口
 */
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/crdt")
public class CreditController {
    @Resource
    private FiCasesServiceCrd ficasesServiceCrd;
    @Resource
    private CreditCardService creditCardService;
    @Resource
    private CreditAccountService creditAccountService;
    @Resource
    private CreditAccountAmortService creditAccountAmortService;
    @Resource
    private FiTaskServiceCrd fiTaskService;
    @Resource
    private CreditAccountBillhistService creditAccountBillhistService;
    @Resource
    private JCommonData jCommonData;
    @Resource
    private CreditAccountEventDtlService creditAccountEventDtlService;
    @Resource
    private CreditAccountNoutbillService creditAccountNoutbillService;

    /**
     * 取案件池GRID
     */
    @PostMapping("/crdCaseList")
    public R<PageData<CrdCaseResVo>> crdCaseList(@RequestBody @Validated CrdCaseReqVo crdCaseReqVo){
        JSysUser user = jCommonData.getCurrUsr();
        if(JPubUtils.IsEqual(crdCaseReqVo.getIsdistquy(),"1")){//2024-06-09 add
            //在待分案查询条件需要保存，为了分案时取
            String key=jCommonData.getDistQuyKey(user.getUsrid());
            //有效期2小时
            boolean b=jCommonData.setData(key,crdCaseReqVo,JPubUtils.distExpiration);
            log.info("crdCaseList.setDistQuyKey({},{}):ret={}",key,crdCaseReqVo,b);
        }
        else if(JPubUtils.IsEqual(crdCaseReqVo.getIsrecyquy(),"1")){//2024-07-28 add
            //在案件池一键回收查询条件需要保存，为了回收时取
            String key=jCommonData.getRecyQuyKey(user.getUsrid());
            //有效期2小时
            boolean b=jCommonData.setData(key,crdCaseReqVo,JPubUtils.distExpiration);
            log.info("crdCaseList.setRecyQuyKey({},{}):ret={}",key,crdCaseReqVo,b);
        }
        else if(JPubUtils.IsEqual(crdCaseReqVo.getIsassquy(),"1")){//2024-08-27 add
            //待派工一键派工操作
            String key=jCommonData.getAssQuyKey(user.getUsrid());
            //有效期2小时
            boolean b=jCommonData.setData(key,crdCaseReqVo,JPubUtils.distExpiration);
            log.info("crdCaseList.setAssQuyKey({},{}):ret={}",key,crdCaseReqVo,b);
        }
        return R.SuccessPage(ficasesServiceCrd.crdCaseList(user,crdCaseReqVo));
    }

    /**
     * 取查询案件欠款汇总金额（已出账单欠款）
     */
    @PostMapping("/getQueryDebtSum")
    public R<QuyAmtSumVo> getQueryDebtSum(@RequestBody @Validated CrdCaseReqVo crdCaseReqVo){
        JSysUser user = jCommonData.getCurrUsr();
        return R.Success(ficasesServiceCrd.getQueryDebtSum(user,crdCaseReqVo));
    }

    /**
     * 取案件详细信息
     */
    @GetMapping("/crdCaseInfo")
    public R<CrdCaseInfoVo> crdCaseInfo(@RequestParam @NotNull String ficaseid){
        JSysUser user = jCommonData.getCurrUsr();
        return R.Success(ficasesServiceCrd.getCrdCaseInfo(user,Integer.parseInt(ficaseid)));
    }

    /**
     * 取案件账户信息GRID
     */
    @GetMapping("/crdAccountList")
    public R<PageData<CrdAccResVo>> crdAccountList(@Validated CrdAccReqVo crdAccReqVo){
        JSysUser user = jCommonData.getCurrUsr();
        return R.SuccessPage(creditAccountService.crdAccountList(user,crdAccReqVo));
    }

    /**
     * 取客户卡片信息GRID
     */
    @GetMapping("/crdCardList")
    public R<PageData<CrdCardResVo>> crdCardList(@Validated CrdCardReqVo crdCardReqVo){
        JSysUser user = jCommonData.getCurrUsr();
        return R.SuccessPage(creditCardService.crdCardList(user,crdCardReqVo));
    }

    /**
     * 取账单分期信息GRID
     */
    @GetMapping("/crdAmortList")
    public R<PageData<CrdAmortResVo>> crdAmortList(@Validated CrdAmortReqVo crdAmortReqVo){
        return R.SuccessPage(creditAccountAmortService.crdAmortList(crdAmortReqVo));
    }

    /**
     * 分配案件到部门或机构
     */
    @PostMapping("/fiCaseDistribute")
    public R<List<DistResVo>> fiCaseDistribute(@RequestBody @Validated DistReqVo distReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        String $usrid= user.getUsrid();
        CrdCaseReqVo quyparas=null;
        if(JPubUtils.IsEqual(distReqVo.getIsquydist(),"1")){//2024-06-09 一键分案
            String key=jCommonData.getDistQuyKey($usrid);
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseDistribute:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(CrdCaseReqVo)o;
                if(!JPubUtils.IsEqual(quyparas.getCcc(), EnumFiCaseQueue.O.name)){
                    //2024-09-27 异常检查，为了不使用全部数据
                    log.warn("fiCaseDistribute:ccc Error={}",quyparas.getCcc());
                    return R.Error(EnumRCode.ArgErr);
                }
            }
        }
        return R.Success(ficasesServiceCrd.crdCaseDistribute(user,distReqVo,quyparas));
    }

    /**
     * 分配案件到催员(派工)
     */
    @PostMapping("/fiCaseAssgin")
    public R<List<DistResVo>> fiCaseAssgin(@RequestBody @Validated AssReqVo assReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        String $usrid= user.getUsrid();
        CrdCaseReqVo quyparas=null;
        if(JPubUtils.IsEqual(assReqVo.getIsquydist(),"1")){//2024-06-09 一键分案
            String key="";
            EnumFiCaseQueue qqq=EnumFiCaseQueue.Q; //2024-10-28
            if(JPubUtils.IsNotEmpty(assReqVo.getGrpid())){//直接派工=EnumFiCaseQueue.O
                qqq=EnumFiCaseQueue.O;
                key=jCommonData.getDistQuyKey($usrid);
            }else {
                key=jCommonData.getAssQuyKey($usrid);
            }
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseAssgin:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(CrdCaseReqVo)o;
                if(!JPubUtils.IsEqual(quyparas.getCcc(), qqq.name)){
                    //2024-09-27 异常检查，为了不使用全部数据
                    log.warn("fiCaseAssgin:ccc Error={}",quyparas.getCcc());
                    return R.Error(EnumRCode.ArgErr);
                }
            }
        }
        return R.Success(ficasesServiceCrd.crdCaseAssign(user,assReqVo,quyparas));
    }

    /**
     * 回收案件
     */
    @PostMapping("/fiCaseRecycle")
    public R<CmdResVo> fiCaseRecycle(@RequestBody @Validated RecyReqVo recyReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        String $usrid= user.getUsrid();
        CrdCaseReqVo quyparas=null;
        if(JPubUtils.IsEqual(recyReqVo.getIsquyrecy(),"1")){//2024-07-28 一键回收
            String key=jCommonData.getRecyQuyKey($usrid);
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseRecycle:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(CrdCaseReqVo)o;
            }
        }
        return R.Success(ficasesServiceCrd.crdCaseRecycle(user,recyReqVo,quyparas));
    }

    /**
     * 转派案件到委外机构
     */
    @PostMapping("/fiCaseTranDist")
    public R<CmdResVo> fiCaseTranDist(@RequestBody @Validated TranDistReqVo tranDistReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        String $usrid= user.getUsrid();
        CrdCaseReqVo quyparas=null;
        if(JPubUtils.IsEqual(tranDistReqVo.getIsquydist(),"1")){
            String key=jCommonData.getDistQuyKey($usrid);
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseTranDist:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(CrdCaseReqVo)o;
                if(!JPubUtils.IsEqual(quyparas.getCcc(), EnumFiCaseQueue.O.name)){
                    //2024-09-27 异常检查，为了不使用全部数据
                    log.warn("fiCaseTranDist:ccc Error={}",quyparas.getCcc());
                    return R.Error(EnumRCode.ArgErr);
                }
            }
        }
        return R.Success(ficasesServiceCrd.crdCaseTranDist(user,tranDistReqVo,quyparas));
    }

    /**
     * 取客户下最新激活的卡号
     */
    @GetMapping("/getCustOpenLastCardNo")
    public R<CmdResVo> getCustOpenLastCardNo(@RequestParam @NotNull String custnumber){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(creditCardService.getCustOpenLastCardNo(user,custnumber));
    }

    /**
     * 取账户下最新激活的卡号
     */
    @GetMapping("/getAccOpenLastCardNo")
    public R<CmdResVo> getAccOpenLastCardNo(@RequestParam @NotNull String acctno){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(creditCardService.getAccOpenLastCardNo(user,acctno));
    }


    /**
     * 卡冻结指令
     */
    @PostMapping("/crdCardFrozen")
    public R<CrdCmdResVo> crdCardFrozen(@RequestBody @Validated CrdFrozenReqVo crdFrozenReqVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.crdCardFrozen(usrid,crdFrozenReqVo));
    }

    /**
     * 卡划扣指令
     */
    @PostMapping("/crdCardDeduction")
    public R<CrdCmdResVo> crdCardDeduction(@RequestBody @Validated CrdDeduReqVo crdDeduReqVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.crdCardDeduction(usrid,crdDeduReqVo));
    }

    /**
     * 查询借记卡余额
     */
    @GetMapping("/getCrdAcctBalance")
    public R<CrdCmdResVo> getCrdAcctBalance(@RequestParam @NotNull Integer ficaseid,String cardno){
        return R.Success(ficasesServiceCrd.getCrdAcctBalance(ficaseid,cardno));
    }

    /**
     * 取历史账单Grid
     */
    @GetMapping("/getCrdAcctBillHist")
    public R<PageData<CrdBillHistResVo>> getCrdAcctBillHist(@Validated CrdBillHistReqVo crdBillHistReqVo){
        return R.SuccessPage(creditAccountBillhistService.crdBillHistList(crdBillHistReqVo));
    }

    /**
     * 刷新账户余额信息
     */
    @PostMapping("/crdRefreshBalance")
    public R<CrdCmdResVo> crdRefreshBalance(@RequestBody @Validated CrdRefreshBalVo crdRefreshBalVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(ficasesServiceCrd.refreshBalance(user,crdRefreshBalVo));
    }

    /**
     * 刷新未出账单信息
     */
    @PostMapping("/crdRefreshNoOutBill")
    public R<CrdCmdResVo> crdRefreshNoOutBill(@RequestBody @Validated CrdRefNOutBillVo crdRefNOutBillVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(ficasesServiceCrd.refreshNoOutBill(user,crdRefNOutBillVo));
    }

    /**
     * 取历史账单明细GRID
     */
    @GetMapping("/crdOutBillList")
    public R<PageData<CrdBillTailResVo>> crdOutBillList(@Validated CrdBillTailReqVo crdBillTailReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(creditAccountEventDtlService.crdEventBillList(user,crdBillTailReqVo));
    }

    /**
     * 取未出账单
     */
    @GetMapping("/crdNOutBill")
    public R<CrdNOutBillResVo> crdNOutBill(@RequestParam @NotNull String  acctno){
        return R.Success(creditAccountNoutbillService.crdNOutBill(acctno));
    }

    /**
     * 取未出账单明细GRID
     */
    @GetMapping("/crdNOutBillList")
    public R<PageData<CrdNOBillTailResVo>> crdNOutBillList(@Validated CrdNOBillTailReqVo crdNOBillTailReqVo){
        return R.SuccessPage(creditAccountNoutbillService.crdNOutBillList(crdNOBillTailReqVo));
    }

    /**
     * 查询AI推送名单
     */
    @GetMapping("/queryAiList")
    public R<PageData<AiListResVo>> queryAiList(@Validated AiListReqVo aiListReqVo){
        return R.SuccessPage(creditAccountService.queryAiList(aiListReqVo));
    }

    /**
     * 推送AICall
     */
    @PostMapping("/sendAiList")
    public R<CmdResVo> sendAiList(@RequestBody @Validated AiListSendVo aiListSendVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.sendAiList(usrid,aiListSendVo));
    }

    /**
     * 回写AICall结果
     */
    @PostMapping("/saveAiListResult")
    public R<CmdResVo> saveAiListResult(@RequestBody @Validated AiCallRetVo aiCallRetVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.saveAiListResult(usrid,aiCallRetVo));
    }

    /**
     * 案件导出
     */
    @PostMapping("/expFiCase")
    public R<CmdResVo> expFiCase(@RequestBody @Validated CrdCaseReqVo crdCaseReqVo, HttpServletResponse response){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(ficasesServiceCrd.crdCaseSaveAs(user,crdCaseReqVo,response));
    }

    /**
     * 取一键回收案件数
     */
    @GetMapping("/getOneRecyCount")
    public R<CmdResVo> getOneRecyCount(){
        JSysUser user= jCommonData.getCurrUsr();
        String key=jCommonData.getRecyQuyKey(user.getUsrid());
        Object o=jCommonData.getData(key);
        CrdCaseReqVo quyparas=null;
        if(o==null){
            log.warn("getOneRecyCount:nofind {}",key);
            return R.Error(EnumRCode.NoFindQuyPara);
        }else{
            quyparas=(CrdCaseReqVo)o;
        }
        return R.Success(ficasesServiceCrd.getOneRecyCount(user,quyparas));
    }
}
